import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { NoticeService } from '../../services/admin/notice.service';
import { SmsService } from '../../services/admin/sms.service';

/**
 * NoticeController
 * 对应 PHP: Notice Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('notice')
@Controller('adminapi/notice')
export class NoticeController {
  constructor(
    private readonly noticeService: NoticeService,
    private readonly smsService: SmsService,
  ) {}

  /**
   * 消息管理
   * 路由: GET notice
   * PHP路由: Route::get('notice', 'notice.Notice/lists')
   */
  @Get('notice')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.noticeService.getList();
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: GET notice/:key
   * PHP路由: Route::get('notice/:key', 'notice.Notice/info')
   */
  @Get('notice/:key')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async info(@Param('key') key: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.noticeService.getInfo(key);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: POST notice/editstatus
   * PHP路由: Route::post('notice/editstatus', 'notice.Notice/editStatus')
   */
  @Post('notice/editstatus')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async editStatus(@Body() data: EditStatusDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: editStatus

      return await this.noticeService.editMessageStatus(
        data['key'],
        data['type'],
        data['status'],
      );
    } catch (error) {
      throw new BusinessException('editStatus操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: GET notice/sms
   * PHP路由: Route::get('notice/sms', 'notice.Notice/smsList')
   */
  @Get('notice/sms')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async smsList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: smsList

      return await this.smsService.getList();
    } catch (error) {
      throw new BusinessException('smsList操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: GET notice/sms/:sms_type
   * PHP路由: Route::get('notice/sms/:sms_type', 'notice.Notice/smsConfig')
   */
  @Get('notice/sms/:sms_type')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async smsConfig(@Param('sms_type') sms_type: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: smsConfig

      return await this.smsService.getConfig(sms_type);
    } catch (error) {
      throw new BusinessException('smsConfig操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: PUT notice/sms/:sms_type
   * PHP路由: Route::put('notice/sms/:sms_type', 'notice.Notice/editSms')
   */
  @Put('notice/sms/:sms_type')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async editSms(
    @Param('sms_type') sms_type: string,
    @Body() data: EditSmsDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: editSms
      const data = { search: '' };
      return await this.notice.getType(sms_type);
    } catch (error) {
      throw new BusinessException('editSms操作失败', error);
    }
  }

  /**
   * 消息管理
   * 路由: POST notice/edit
   * PHP路由: Route::post('notice/edit', 'notice.Notice/edit')
   */
  @Post('notice/edit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '消息管理' })
  async edit(@Body() data: EditDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.noticeService.edit(data['key'], data['type'], data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }
}
